package com.weic.easycache.utils;

import android.text.TextUtils;

public class FileNameUtils {
	public static final byte UNKNOWN	= -1;
	public static final byte AUDIO		= 0;
	public static final byte VIDEO		= 1;
	public static final byte EDUNOTE	= 2;
	public static final String[] VIDEO_OFF_LINE		= new String[] {".edu4"};
	public static final String[] VIDEO_ON_LINE		= new String[] {".mp4", ".f4v", ".flv", ".m3u8"};
	public static final String[] VIDEO_SUFFIXS		= new String[] {".mp4", ".f4v", ".flv", ".m3u8", ".edu4", ".3gp", ".avi", ".rmvb", ".rm"};
	public static final String[] AUDIO_SUFFIXS		= new String[] {".mp3", ".wav", ".wma", ".mid", ".vqf", "ogg"};
	public static final String[] EDUNOTE_SUFFIXS	= new String[] {".html", ".htm", ".xhtml", ".pdf"};
	public static final String[] PICTURE_SUFFIXS	= new String[] {".jpg", ".jpeg", ".gif", ".png"};

	public static byte resolveFileType(String fileName) {
		byte type = UNKNOWN;
		if(!TextUtils.isEmpty(fileName)) {
			if(isVideo(fileName)) {
				type = VIDEO;
			}else if(isAudio(fileName)) {
				type = AUDIO;
			}else if(isEduNote(fileName)) {
				type = EDUNOTE;
			}
		}
		return type;
	}

	public static boolean isVideo(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : VIDEO_SUFFIXS) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isOnlineVideo(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : VIDEO_ON_LINE) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}

	public static boolean isOfflineVideo(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : VIDEO_OFF_LINE) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isAudio(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : AUDIO_SUFFIXS) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isEduNote(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : EDUNOTE_SUFFIXS) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isPicture(String fileName) {
		String name = fileName.toLowerCase();
		for(String suff : PICTURE_SUFFIXS) {
			if(name.endsWith(suff)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 从Url中获取文件名
	 */
	public static String getFileNameFromUrl(String url) {
		String filename = url.substring(url.lastIndexOf('/')+1);
		if(filename.indexOf('?')>0) filename = filename.substring(0, filename.indexOf('?'));
		return filename;
	}
}
